package allwe.thread02;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class BuyTicketThread implements Runnable{
    private int ticketNum = 10;

    Lock lock = new ReentrantLock();

    @Override
    public void run() {
        //每个窗口有100个人抢票
        //每个线程抢票100次
        for (int i = 0; i < 100; i++) {
            lock.lock();
            buyTicket();
            lock.unlock();
        }
    }

    public void buyTicket(){
        if (ticketNum > 0)
            System.out.println(Thread.currentThread().getName() + "买到车票============" + ticketNum--);
    }
}
